package com.jrx.anytxn.investor.controller.cms;

import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.investor.dto.loan.*;
import com.jrx.anytxn.investor.service.ILoanAccountInvestorInfoService;
import com.jrx.anytxn.investor.service.ILoanMainInvestorInfoService;
import com.jrx.anytxn.investor.service.IRepaymentPlanInvestorInfoService;
import com.jrx.anytxn.investor.service.ITLFundRouteLogSerivce;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 *为出资方提供查询订单相关数据
 * @author 黄秋平
 * @create 2020/6/18
 */
@Api(tags = "出资方相关订单以及账户信息")
@RestController
@RequestMapping(value = "/investor/investorOrder")
public class InvestorOrderController {

    private static final Logger logger = LoggerFactory.getLogger(InvestorOrderController.class);

    @Resource
    private InfoService infoService;

    @Resource
    private ILoanAccountInvestorInfoService loanAccountInvestorInfoService;

    @Resource
    private ILoanMainInvestorInfoService loanMainInvestorInfoService;

    @Resource
    private IRepaymentPlanInvestorInfoService repaymentPlanInvestorInfoService;

    @Resource
    private ITLFundRouteLogSerivce tlFundRouteLogSerivce;



    @ApiOperation(value = "订单详情的账户信息查询list", notes = "订单详情的账户信息查询list")
    @GetMapping("/getLoanAccountInvestorList/{loanId}/{customerId}/{investorId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "loanId", value = "订单ID", required = true),
            @ApiImplicitParam(name = "customerId", value = "客户ID", required = true),
            @ApiImplicitParam(name = "investorId", value = "资金方ID", required = true)})
    public TxnRespResult<List<LoanAccountInvestorInfoRes>> getLoanAccountInvestorList(@PathVariable String loanId, @PathVariable String customerId, @PathVariable String investorId){
        TxnRespResult<List<LoanAccountInvestorInfoRes>> result = new TxnRespResult<>();
        String tenantId= String.valueOf(infoService.getUser().getProjectId());
        try {
            result.getSuccess(loanAccountInvestorInfoService.getAccountByTenIdAndCusIdAndLoanIdAndInveId(tenantId, customerId,loanId,investorId));
        } catch (TxnBizException e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), e);
        }
        return result;
    }

    @ApiOperation(value = "订单详情的还款计划查询list", notes = "订单详情的还款计划查询list")
    @GetMapping("/getRepaymentPlanInvestorList/{loanId}/{customerId}/{investorId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "loanId", value = "订单ID", required = true),
            @ApiImplicitParam(name = "customerId", value = "客户ID", required = true),
            @ApiImplicitParam(name = "investorId", value = "资金方ID", required = true)})
    public TxnRespResult<List<AmRepaymentPlanInvestorInfoRes>> getRepaymentPlanInvestorList(@PathVariable String loanId, @PathVariable String customerId, @PathVariable String investorId){
        TxnRespResult<List<AmRepaymentPlanInvestorInfoRes>> result = new TxnRespResult<>();
        String tenantId= String.valueOf(infoService.getUser().getProjectId());
        try {
            result.getSuccess(repaymentPlanInvestorInfoService.getRepaymentListByLoanIdAndCusIdAndInveId(tenantId, customerId,loanId,investorId));
        } catch (TxnBizException e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), e);
        }
        return result;
    }

    @ApiOperation(value = "订单详情的账户信息明细查询", notes = "订单详情的账户信息明细查询")
    @GetMapping(value = "/getLoanAccountInvestorModel/{investorLoanId}/{customerId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "investorLoanId", value = "出资方贷款订单id", required = true),
            @ApiImplicitParam(name = "customerId", value = "客户ID", required = true)})
    public TxnRespResult<LoanAccountInvestorInfoRes> getLoanAccountInvestorModel(@PathVariable("investorLoanId") String investorLoanId, @PathVariable("customerId") String customerId) {
        TxnRespResult<LoanAccountInvestorInfoRes> result = new TxnRespResult();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            LoanAccountInvestorInfoRes loanAccountInfoRes=loanAccountInvestorInfoService.getLoanAccountByInveLoanIdAndCusId(tenantId,customerId,investorLoanId);
            result = result.getSuccess(loanAccountInfoRes);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), e);
        }
        return result;
    }


    @ApiOperation(value = "订单详情的还款计划明细查询", notes = "订单详情的还款计划明细查询")
    @GetMapping(value = "/getRepaymentPlanInvestorModel/{investorLoanId}/{customerId}/{billingTenor}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "investorLoanId", value = "出资方贷款订单id", required = true),
            @ApiImplicitParam(name = "customerId", value = "客户ID", required = true),
            @ApiImplicitParam(name = "billingTenor", value = "期数", required = true)
    })
    public TxnRespResult<AmRepaymentPlanInvestorInfoRes> getRepaymentPlanInvestorModel(@PathVariable("investorLoanId") String investorLoanId, @PathVariable("customerId") String customerId, @PathVariable("billingTenor") int billingTenor) {
        TxnRespResult<AmRepaymentPlanInvestorInfoRes> result = new TxnRespResult();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            result = result.getSuccess(repaymentPlanInvestorInfoService.getRepaymentInfoByInvesLoanIdAndCusIdAndTenor(tenantId,customerId,investorLoanId,billingTenor));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), e);
        }
        return result;
    }

    @ApiOperation(value = "根据出资方贷款订单ID和客户ID查看利率参数信息", notes = "根据出资方贷款订单ID和客户ID查看利率参数信息")
    @GetMapping(value = "/getInvestorInterestModel/{investorId}/{customerId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "investorId", value = "出资方id", required = true),
            @ApiImplicitParam(name = "customerId", value = "客户ID", required = true),
    })
    public TxnRespResult<LoanMainInvestorInfoRes> getInvestorInterestModel(@PathVariable("investorId") String investorId, @PathVariable("customerId") String customerId) {
        TxnRespResult<LoanMainInvestorInfoRes> result = new TxnRespResult();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            LoanMainInvestorInfoRes res=loanMainInvestorInfoService.getInfoByInveLoanIdAndCusId(tenantId,customerId,investorId);
            result = result.getSuccess(res);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), e);
        }
        return result;
    }


    @ApiOperation(value = "根据订单ID和客户ID和资金源ID查看出资详情", notes = "根据订单ID和客户ID和资金源ID查看出资详情")
    @GetMapping(value = "/getFundAndInvestorInfo/{loanId}/{customerId}/{fundId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "loanId", value = "订单号", required = true),
            @ApiImplicitParam(name = "customerId", value = "客户ID", required = true),
            @ApiImplicitParam(name = "fundId", value = "资金源ID", required = true),
    })
    public TxnRespResult<FundInfoRes> getFundAndInvestorInfo(@PathVariable("loanId") String loanId, @PathVariable("customerId") String customerId, @PathVariable("fundId") String fundId) {
        TxnRespResult<FundInfoRes> result = new TxnRespResult();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            FundInfoRes res=loanMainInvestorInfoService.getFundAndInvestorInfo(tenantId,loanId,fundId,customerId);
            result = result.getSuccess(res);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), e);
        }
        return result;
    }


    @ApiOperation(value = "根据客户ID查看资金方路由信息", notes = "根据客户ID查看资金方路由信息v")
    @GetMapping(value = "/getFundRouteLogsModel/{customerId}/{seqNo}")
    @ApiImplicitParam(name = "customerId", value = "客户ID", required = true)
    public TxnRespResult<List<FundRouteLogRes>> getFundRouteLogsModel(@PathVariable("customerId") String customerId,@PathVariable("seqNo") String seqNo) {
        TxnRespResult<List<FundRouteLogRes>> result = new TxnRespResult();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            List<FundRouteLogRes> res=tlFundRouteLogSerivce.getFundRouteLogsByFundIdAndCusId(tenantId,customerId,seqNo);
            result = result.getSuccess(res);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), e);
        }
        return result;
    }








}
